import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;


import javax.swing.ImageIcon;
import javax.swing.JPanel;


public class P01_Panel extends JPanel  implements MouseListener, MouseMotionListener {
	
	private PanelListener listener;
	
	//1920
	private int width=0;
	//1080
	private int height=0;
	
	//menu control
	private Image[] imageMenu = new Image[9];
	private boolean[] menu_presseed = new boolean[4];
	private boolean help_pressed = false;
	
	private Image[] imageList = new Image[14];
	
	private int position=0;
	private List<Age_Position> age_position= new ArrayList<Age_Position>();
	
	/*
	 * Calendar
	 * 
	 * [x]
	 * 445
	 * 611
	 * 777
	 * 943
	 * 1109
	 * 1275
	 * [y]
	 * 351
	 */
	
	private int[] xpos = {445,611,777,943,1109,1275};
	
	private boolean moving=false;
	
	public P01_Panel(int width, int height){
		
		this.width=width;
		this.height=height;
		
		//menu control
		imageMenu[0]=new ImageIcon(getClass().getResource("images/m/h0.png")).getImage();
		imageMenu[1]=new ImageIcon(getClass().getResource("images/m/h1.png")).getImage();
		imageMenu[2]=new ImageIcon(getClass().getResource("images/m/f0.png")).getImage();
		imageMenu[3]=new ImageIcon(getClass().getResource("images/m/f1.png")).getImage();
		imageMenu[4]=new ImageIcon(getClass().getResource("images/m/b0.png")).getImage();
		imageMenu[5]=new ImageIcon(getClass().getResource("images/m/b1.png")).getImage();
		imageMenu[6]=new ImageIcon(getClass().getResource("images/m/q0.png")).getImage();
		imageMenu[7]=new ImageIcon(getClass().getResource("images/m/q1.png")).getImage();
		imageMenu[8]=new ImageIcon(getClass().getResource("images/m/help/01.png")).getImage();
		
		menu_presseed[0]=false;
		menu_presseed[1]=false;
		menu_presseed[2]=false;
		menu_presseed[3]=false;
		
		
		imageList[0]=new ImageIcon(getClass().getResource("images/1/background.png")).getImage();
		imageList[1]=new ImageIcon(getClass().getResource("images/1/10.png")).getImage();
		imageList[2]=new ImageIcon(getClass().getResource("images/1/20.png")).getImage();
		imageList[3]=new ImageIcon(getClass().getResource("images/1/30.png")).getImage();
		imageList[4]=new ImageIcon(getClass().getResource("images/1/40.png")).getImage();
		imageList[5]=new ImageIcon(getClass().getResource("images/1/50.png")).getImage();
		imageList[6]=new ImageIcon(getClass().getResource("images/1/60.png")).getImage();
		imageList[7]=new ImageIcon(getClass().getResource("images/1/Calendar.png")).getImage();
		imageList[8]=new ImageIcon(getClass().getResource("images/1/10_.png")).getImage();
		imageList[9]=new ImageIcon(getClass().getResource("images/1/20_.png")).getImage();
		imageList[10]=new ImageIcon(getClass().getResource("images/1/30_.png")).getImage();
		imageList[11]=new ImageIcon(getClass().getResource("images/1/40_.png")).getImage();
		imageList[12]=new ImageIcon(getClass().getResource("images/1/50_.png")).getImage();
		imageList[13]=new ImageIcon(getClass().getResource("images/1/60_.png")).getImage();
		
		
		addMouseListener(this);
		addMouseMotionListener(this);
	  	Dimension size = new Dimension(width, height);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		setSize(size);
		setLayout(null);
		
		
		age_position.add(new Age_Position(497, 497, 593, 582));
		age_position.add(new Age_Position(663, 497, 758, 582));
		age_position.add(new Age_Position(829, 497, 924, 582));
		age_position.add(new Age_Position(995, 497, 1095, 582));
		age_position.add(new Age_Position(1161, 497, 1257, 582));
		age_position.add(new Age_Position(1327, 497, 1423, 582));
		
	}
	  

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//draw background
		g.drawImage(imageList[0], 0, 0, null);
		
		//draw menu control

		//home
		if(menu_presseed[0]){
			g.drawImage(imageMenu[1], 1800, 20, null);
		}else{
			g.drawImage(imageMenu[0], 1800, 20, null);
		}
		
		
		//back
		
		if(menu_presseed[1]){
			g.drawImage(imageMenu[5], 20, 950, null);
		}else{
			g.drawImage(imageMenu[4], 20, 950, null);
		}
		
		
		//forward
		if(menu_presseed[2]){
			g.drawImage(imageMenu[3], 1830, 950, null);
		}else{
			g.drawImage(imageMenu[2], 1830, 950, null);
		}
		
		//help
		if(menu_presseed[3]){
			g.drawImage(imageMenu[7], 1732, 20, null);
		}else{
			g.drawImage(imageMenu[6], 1732, 20, null);
		}
		
		//help content
		if(help_pressed){
			g.drawImage(imageMenu[8], 1365, 79, null);
		}
				
		/*
		 * Age
		 * 
		 * [Width]
		 * 96
		 * [Height]
		 * 85
		 */
		
		
		for(int i=0;i<6;i++){
			if(i==position){
				g.drawImage(imageList[i+1], age_position.get(i).xpos1, age_position.get(i).ypos1, null);
			}else{
				if(i!=3){
					g.drawImage(imageList[i+8], age_position.get(i).xpos1, age_position.get(i).ypos1, null);
				}else{
					g.drawImage(imageList[i+8], age_position.get(i).xpos1, age_position.get(i).ypos1-1, null);
				}
				
			}
		}
		
		/*
		 * Calendar
		 * 
		 * [x]
		 * 445
		 * 611
		 * 777
		 * 943
		 * 1109
		 * 1275
		 * [y]
		 * 351
		 */
		
		g.drawImage(imageList[7], xpos[position], 351, null);
		
		
	}
	
	public void mousePressed(MouseEvent e) {
		
		//home
		if(1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			menu_presseed[0]=true;
		}
		
		//back
		if(20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[1]=true;
		}
		
		
		//forward
		if(1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			menu_presseed[2]=true;
		}
		
		//help
		if(1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			menu_presseed[3]=true;
		}
				
		
		for(int i=0;i<age_position.size();++i){
			if(age_position.get(i).xpos1<=e.getX() && e.getX()<=age_position.get(i).xpos2 
					&& age_position.get(i).ypos1<=e.getY() && e.getY()<=age_position.get(i).ypos2){
				position=i;
				break;
			}
		}
		
		
		this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
    	
    	if(menu_presseed[0] && 1800<=e.getX() && e.getX()<=1800+63 
				&& 40<=e.getY() && e.getY()<=40+57){
			//home
    		if(!moving){
    			listener.onHome();
        		moving=true;
    		}
		}else if(menu_presseed[1] && 20<=e.getX() && e.getX()<=20+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//back
			if(!moving){
				listener.onBack();
				moving=true;
    		}
			
		}else if(menu_presseed[2] && 1830<=e.getX() && e.getX()<=1830+68 
				&& 950<=e.getY() && e.getY()<=950+108){
			//forward
			if(!moving){
				listener.onChangeValue(1, position);
				listener.onForward();
				moving=true;
			}
		}else if(menu_presseed[3] && 1732<=e.getX() && e.getX()<=1732+60 
				&& 20<=e.getY() && e.getY()<=20+60){
			help_pressed=!help_pressed;
		}
    	
    	menu_presseed[0]=false;
    	menu_presseed[1]=false;
    	menu_presseed[2]=false;
		menu_presseed[3]=false;
    	
    	this.repaint();
    }
    
    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
    	
    }
    
    public void mouseMoved(MouseEvent e) {
        
    	
    }
    
    public void mouseDragged(MouseEvent e) {
        
    	for(int i=0;i<age_position.size();++i){
			if(age_position.get(i).xpos1<=e.getX() && e.getX()<=age_position.get(i).xpos2 
					&& age_position.get(i).ypos1<=e.getY() && e.getY()<=age_position.get(i).ypos2){
				position=i;
				break;
			}
		}
    	
    	this.repaint();
    }
    
    public void setPanelListener(PanelListener listener){
    	this.listener=listener;
    }
}